<?php
namespace Home\Controller;

use Think\Controller;
use Home\Model;


///淘宝好券采集
//http://localhost/index/Itemcaiji/index
class ItemcaijiController extends FuncController
{
    public function _initialize()
    {

        $this->_mod = D('Home/Items');
    }

    //http://localhost/Home/Itemcaiji/index
    public function index()
    {
        $this->show("index/Itemcaiji/index：hello1 1world!");
    }

    const TOKEN = 'ssf.API';

	//响应前台的请求
    public function service()
    {
        if (IS_POST) {
       //验证身份
            $timeStamp = I('t', '', 'trim');
            $signature = I('s', '', 'trim');

            if ($signature != 'ssf.API') {
                $this->ajaxReturn(0, L('授权错误'));
            }
            //$str = $this->arithmetic($timeStamp, $randomStr);
       // if($str != $signature){
       //     echo "-1";
       //     exit;
       // }
       //模拟数据
            $reqData = I('d', '', 'trim');
            $item = json_decode($reqData);
            $totalnum = count($item);
            $totalcoll = 0;
            $coll = 0;
            for ($i = 0; $i < $totalnum; $i++) {
                $_item['cate_id'] = $item[$i]->cid;

                $_item['num_iid'] = $item[$i]->num_iid;

                $_item['zc_id'] = $item[$i]->taocid;
                $_item['zkType'] = $item[$i]->taocName;

                if ($_item['num_iid'] < 1) break;

                $_item['title'] = $item[$i]->title;
                $_item['tags'] = $item[$i]->tags;
                $_item['sellerId'] = $item[$i]->sellerId;
                if (false === strpos($item[$i]->pic_url, 'http'))
                    $_item['pic_url'] = 'http:' . $item[$i]->pic_url;
                else
                    $_item['pic_url'] = $item[$i]->pic_url;

                $picUrls = "";
                foreach ($item[$i]->small_images as $url) {
                    $picUrls .= $url . ',';
                }
           // 使用 rtrim() 函数从字符串右端删除字符：
                $picUrls = rtrim($picUrls, ',');
                $_item['pic_urls'] = $picUrls;
                $_item['price'] = $item[$i]->good_price != '' ? $item[$i]->good_price : $item[$i]->good_discountprice;
                $_item['volume'] = $item[$i]->sale_num == '' ? 0 : $item[$i]->sale_num;
                $_item['coupon_price'] = $item[$i]->good_discountprice;
                $_item['coupon_rate'] = round(($_item['coupon_price'] / $_item['price']) * 10000, 0);
                $_item['shop_type'] = $item[$i]->shop_type == 1 ? 'B' : 'C';
                $_item['item_url'] = $item[$i]->item_url;
                $_item['add_time'] = time();
                $_item['isq'] = 1;
                $_item['quan'] = $item[$i]->good_couponprice;
                $_item['quanurl'] = $item[$i]->coupon_url;
                $_item['quan_surplus'] = $item[$i]->coupon_remain;
                $_item['quan_receive'] = $item[$i]->coupon_total - $item[$i]->coupon_remain;
                $_item['commission'] = $item[$i]->commission;
                $_item['commission_rate'] = $item[$i]->commission_rate;
                $_item['nick'] = $item[$i]->nick;
                $_item['coupon_start_time'] = time();
                $_item['coupon_end_time'] = $item[$i]->coupon_endtime + 24 * 60 * 60 - 1;
           //$_item['ems'] = $item[$i]->ems;
                $_item['astime'] = date('Ymd');
           //$_item['dx'] = $item[$i]->plan_id == 2 ? 1 :0;
           //定向
           //$_item['dx'] =1;

           //推荐
                $_item['tui'] = $item[$i]->isRecommend;

                $_item['intro'] = $item[$i]->good_intro;
                $_item['from'] = $item[$i]->from;
                $_item['click_url'] = $item[$i]->coupon_url;
           //$_item['quan_condition']=$item[$i]->qcon;

                if (0 != $this->_publish_insert($_item))
                    $coll++;
                $totalcoll++;
            }
            $resResult = array();
            $resResult['total'] = $totalcoll;
            $resResult['succ'] = $coll;
            return $this->ajaxReturn(1, L('operation_success'), $resResult);
        }
        return $this->ajaxReturn(0, L('operation_error'));
    }

    private function _publish_insert($item)
    {
        $result = $this->_mod->ajax_itk_publish($item);
        return $result;
    }


    /**
     * @param $timeStamp 时间戳
     * @param $randomStr 随机字符串
     * @return string 返回签名
     */
    private function arithmetic($timeStamp, $randomStr)
    {
        $arr['timeStamp'] = $timeStamp;
        $arr['token'] = self::TOKEN;
        //按照首字母大小写顺序排序
        sort($arr, SORT_STRING);
        //拼接成字符串
        $str = implode($arr);
        //进行加密
        $signature = sha1($str);
        //$signature = md5($signature);
        //转换成大写
        $signature = strtoupper($signature);
        return $signature;
    }


}
?>
